/**
 * Created by zhangguoyu on 16/5/10.
 */

var Utils = require('../utils/Utils');
var MapReduceBuilder = require('../db/MapReduceBuilder');
var BaseLogsResolver = require('./BaseLogsResolver');
var TempLogs = require('../models/logs/TempLogs');
var TempsContent = require('../models/temps/TempsContent');

var coreData = require('./CoreData');

var ContentLogsResolver = Utils.extend(function () {
  if (arguments.length) {
    this.$resolvedModel = arguments[0];
  }
}, BaseLogsResolver, TempLogs);

ContentLogsResolver.prototype.prepareResolve = function (model) {
  return new MapReduceBuilder()
    .map(function () {
      var categoryCode = this.data.categoryCode;
      var contentCode = this.data.contentCode;
      if (!categoryCode || !contentCode) {
        return;
      }
      var key = this.date + '|' + this.time.split(':')[0] + '|' + this.flavor + '|' + this.appPackage + '|' + categoryCode + '|' + contentCode;
      if (this.actionCode === '1200') {
        emit(key, { playCount: 1, durations: 0 });
      } else {
        emit(key, { playCount: 0, durations: parseInt(this.data.duration) });
      }
    })
    .reduce(function (key, values) {
      var reduceRes = {
        playCount: 0,
        durations: 0
      };
      values.forEach(function(item) {
        reduceRes.playCount += item.playCount;
        reduceRes.durations += item.durations;
      });
      return reduceRes;
    })
    .verbose()
    .query({ actionCode: { '$in': ['1200', '2200']}})
    .out({replace: TempsContent.$name.toLowerCase() + 's'})
    .create();
};

ContentLogsResolver.prototype.onResolving = function (date, time, flavor, app, category, content, value) {
  coreData.globalStat().global()
    .incPlayCount(time, flavor, value.playCount)
    .incDurations(time, flavor, value.durations);
  coreData.globalStat().app(app)
    .incPlayCount(time, flavor, value.playCount)
    .incDurations(time, flavor, value.durations);
  coreData.globalStat().category(category, app)
    .incPlayCount(time, flavor, value.playCount)
    .incDurations(time, flavor, value.durations);
  coreData.globalStat().content(content, app)
    .incPlayCount(time, flavor, value.playCount)
    .incDurations(time, flavor, value.durations);
  coreData.dayStat(date).global()
    .incPlayCount(time, flavor, value.playCount)
    .incDurations(time, flavor, value.durations);
  coreData.dayStat(date).app(app)
    .incPlayCount(time, flavor, value.playCount)
    .incDurations(time, flavor, value.durations);
  coreData.dayStat(date).category(category, app)
    .incVisited(time, flavor, value.playCount)
    .incPlayCount(time, flavor, value.playCount)
    .incDurations(time, flavor, value.durations);
  coreData.dayStat(date).content(content, app)
    .incVisited(time, flavor, value.playCount)
    .incPlayCount(time, flavor, value.playCount)
    .incDurations(time, flavor, value.durations);
  this.parent().onResolving.call(this, date, time, flavor, app, category, content, value);
};

ContentLogsResolver.prototype.onCompletion = function (resolvedResult) {
  this.parent().onCompletion.call(this, resolvedResult);
};

module.exports = ContentLogsResolver;